function CommonEquip()
    return EventHandler("equip", function(inst) inst.sg:GoToState("idle") end) 
end

function CommonUnequip()
    return EventHandler("unequip", function(inst) inst.sg:GoToState("idle") end)
end

function CommonAnimover(name)
    return EventHandler("animover",function(inst) inst.sg:GoToState(name or "idle") end)
end

function CommonAnimQueueOver()
    return EventHandler("animqueueover", function(inst)
        if inst.AnimState:AnimDone()then 
            inst.sg:GoToState("idle")
        end
    end)
end

function CommonProjectileUpdate(inst, dt)
    if (inst.sg.statemem.projectiledelay or 0) > 0 then
        inst.sg.statemem.projectiledelay = inst.sg.statemem.projectiledelay - dt
        if inst.sg.statemem.projectiledelay <= 0 then
            if TheWorld.ismastersim then
                inst:PerformBufferedAction()
            else
                inst:ClearBufferedAction()
            end
            inst.sg:RemoveStateTag("abouttoattack")
        end
    end
end


local TOO_CLOSE_WARNING = L and "It's too close!" or "太近了! 会波及到我的!"

local function EventPostInit(self)
    local old_fn = self.actionhandlers[ACTIONS.ATTACK].deststate
    self.actionhandlers[ACTIONS.ATTACK].deststate = function(inst, action)

        local isdead = inst.replica.health and inst.replica.health:IsDead()
        local item = inst.replica.combat and inst.replica.combat:GetWeapon()
        local target = action.target
        local combat = inst.components.combat or inst.replica.combat

        local isblind = inst.replica.homura_lightshocked:IsBlind()

        if isblind and not isdead and target and target.prefab == 'fake_target_player' then
            return 'attack'
        end
        if isdead or not(item and target and item:IsValid() and target:IsValid() and combat) then
            return old_fn(inst, action)
        end

        if item.prefab == "homura_rpg" and item:HasTag('homuraTag_ranged') then
            if target and target:IsValid() and target:GetDistanceSqToInst(inst) <= 4*4 then
                combat:SetTarget(nil)
                if inst.components.talker then
                    if not inst.components.talker.widget and not inst.components.talker.task then
                        inst.components.talker:Say(TOO_CLOSE_WARNING)
                    elseif inst.components.talker.widget ~= nil and inst.components.talker.widget.text:GetString() ~= TOO_CLOSE_WARNING then
                        inst.components.talker:Say(TOO_CLOSE_WARNING)
                    elseif inst.components.locomotor then
                        inst.components.locomotor:Stop()
                    end
                end
                return
            else
                return "homura_rpg"
            end
        end

        if item.prefab == "homura_rifle" then
            if not item:HasTag("homuraTag_ranged") then
                return old_fn(inst, action)
            elseif inst.sg:HasStateTag("homura_sniping") then
                inst.sg.statemem.shoot = true
                return "homura_snipeshoot"
            elseif not inst.sg:HasStateTag("attack") and not inst.sg:HasStateTag("abouttoattack") then
                -- 2021.10.1 for controller
                if target ~= nil then
                    if inst.HUD ~= nil then
                        inst.HUD.homuraUI_focus:PointCursorAtTarget(target)
                    end
                end
                return "homura_sniping"
            end
        end

        if item:HasTag('homuraTag_ranged') then
            if item.prefab == 'homura_pistol' then
                if not inst.sg:HasStateTag('homura_pistol_atk') then
                    return 'homura_pistol_pre'
                else
                    return
                end
            elseif item.prefab == 'homura_gun' then
                if not inst.sg:HasStateTag('homura_gun_atk') then
                    return 'homura_gun_pre'
                --elseif not inst.sg:HasStateTag('attack') then
                --    return 'homura_gun_atk'
                else
                    return
                end
            elseif item.prefab == 'homura_hmg' then
                if not inst.sg:HasStateTag('homura_hmg_atk') then
                    return 'homura_gun_pre'
                --elseif not inst.sg:HasStateTag('attack') then
                --    return 'homura_gun_atk'
                else
                    return
                end
            end
        end

        return old_fn(inst, action)
    end
end

AddStategraphPostInit('wilson', EventPostInit)
AddStategraphPostInit('wilson_client', EventPostInit)

modimport 'modmain/SGrpg.lua'
modimport 'modmain/SGpistol.lua'
modimport 'modmain/SGgun_pre.lua'
modimport 'modmain/SGgun.lua'
modimport "modmain/SGsnipe.lua"